#include<iostream>
using namespace std;
//使用回调函数模拟实现计算器
void menu() 
{
  cout << "************************************" << endl;
  cout << "****** 0.exit  1.Add****************"<<endl;
  cout << "*******2.Sub   3.Mul****************"<<endl;
  cout << "*******4.Div   5.Delivery***********"<<endl; 
  cout << "************************************"<<endl;
}
enum Option
{
  EXIT,
  ADD,
  SUB,
  MUL,
  DIV,
  DELIVERY
};
int Add(int x,int y)
{
    return x + y;
}
int Sub(int x,int y)
{
    return x - y;
}
int Mul(int x,int y)
{
  return x * y;
}
int Div(int x,int y)
{
  if(y == 0)
  {
    cout <<"被除数不能为0"<<endl;
    return -1;
  }
  return x / y;
}
int Delivery(int x,int y)
{
  if(y == 0 )
  {
    cout <<"被除数不能为0" << endl;
    return -1;
  }
  return x % y;
}
typedef int(*pfunc)(int,int);//pfunc是函数指针
void CallBack(pfunc pf)
{
  int x,y;
  cout << "输入两个操作数:";
  cin >> x >> y;
  cout << "计算结果为: " << pf(x,y) << endl;
  cout << "计算结果为: "<< (*pf)(x,y) << endl;
}
int main()
{
    int input = 0; //选择
    do
    {
      menu();
      cout <<"请输入你的选择: ";
      cin >> input;
      switch(input)
      {
        case EXIT:
          cout <<"退出游戏..."<<endl;
          break;
        case ADD:
          CallBack(Add);
          break;
        case SUB:
          CallBack(Sub);
          break;
        case MUL:
          CallBack(Mul);
          break;
        case DIV:
          CallBack(Div);
          break;
        case DELIVERY:
          CallBack(Delivery);
          break;
        default:
          cout <<"输入有误,请重新输入"<<endl;
          break;
      }
    }while(input);
    return 0;
}
